@extends('layouts.app')

@section('content')
<div class="content-header">
    <h1>部署列表</h1>
    <ol class="breadcrumb">
      <li><a href="{{ url('/') }}"><i class="fa fa-dashboard"></i> 首页</a></li>
      <li class="active">部署列表</li>
    </ol>
  </div>
<div class="content">
    @if (\Session::get('success_msg'))
    <div class="alert alert-success" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      {!! \Session::get('success_msg') !!}
    </div>
    @endif
    
    @if (\Session::get('error_msg'))
    <div class="alert alert-danger" role="alert">
      <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
      {!! \Session::get('error_msg') !!}
    </div>
    @endif
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-primary">
                <div class="panel-heading">
                    部署列表
                    <a href="{{url('project/create/'.$project['id'])}}" class="pull-right label label-primary">添加配置</a>
                    <a href="{{url('deploy/'.$project['id'].'/batch')}}" class="pull-right label label-primary">批量部署该项目</a>
                </div>
                <div class="panel-body">
                    @foreach ($setting_list as $setting)
                    <div class="panel panel-primary">
                        <div class="panel-heading">
                           {{ $setting->id }} - {{ $project['path_with_namespace'] }} - {{ $setting->name }}
                        </div>
                        <div class="panel-body">
                            <form class="form-horizontal" method="post" action="{{ url('web_deploy') }}">
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">配置名称</label>
                                    <div class="col-sm-10">
                                        <input type="text" class="form-control" value="{{ $setting->name }}" disabled>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">部署分支</label>
                                    <div class="col-sm-10">
                                        <select class="form-control input-sm" name="branch" id="">
                                            @foreach ($branches as $branch)
                                            <option data-commits="{{ json_encode($branch['commit']) }}" {{ Input::old('branch') ? (Input::old('branch') == $branch['name'] ? 'selected' : '') : ($branch['name'] == $setting->default_branch ? 'selected' : '') }} value="{{ $branch['name'] }}">【分支】{{ $branch['name'] }}</option>
                                            @endforeach
                                            
                                            @foreach ($tags as $tag)
                                            <option data-commits="{{ json_encode($tag['commit']) }}" {{ Input::old('branch') == $tag ? 'selected' :'' }} value="{{ $tag['name'] }}">【Tag】{{ $tag['name']."（".$tag['message']."）" }}</option>
                                            @endforeach
                                        </select>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">部署信息</label>
                                    <div class="col-sm-10">
                                        <div class="panel panel-info" style="font-size: 12px;">
                                            <div class="panel-body">
                                                <p>最后提交人：<span class="last_commit_user">路人甲</span></p>
                                                <p>最后提交信息：<span class="last_commit_message">路人甲</span></p>
                                                <p>最后提交时间：<span class="last_commit_time">路人甲</span></p>
                                                <p>与服务器文件差异：(<a class="gitlab-compare" data-last-sha-id="{{$setting['last_sha_id'] ? :'master'}}" data-web-url="{{$project['web_url']}}" target="_blank" href="#">去 GitLab 查看详细</a>) </p>
                                              @if (!empty($setting->exclude_files))
                                                <p>
                                                    本次部署将会忽略以下文件（目录）:
                                                    {{ $setting->exclude_files }}
                                                </p>
                                                @endif
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label class="col-sm-2 control-label">指定目录名</label>
                                    <div class="col-sm-10">
                                        <textarea name="include_paths" class="form-control" value="{{ Input::old('include_paths','') }}" placeholder="要部署的目录名">{{ Input::old('include_paths') }}</textarea>
                                        <span class="help-block control-label">部署多个目录请换行，默认全量部署</span>
                                    </div>
                                </div>
                                <div class="form-group">
                                    <div class="col-sm-offset-2 col-sm-10">
                                        {!! csrf_field() !!}
                                        <input type="hidden" name="project_id" value="{{$project['id']}}">
                                        <input type="hidden" name="setting_id" value="{{$setting->id}}">
                                        <input type="hidden" name="checkout_sha" value="">
                                        <input type="submit" class="btn btn-info btn-sm" value="执行部署" />
                                    </div>
                                </div>
                            </form>
                            
                        </div>
                        <div class="panel-footer clearfix">
                            
                        </div>
                    </div>
                    @endforeach
                    @if ($setting_list->isEmpty())
                        请先<a href="{{url('project/create/'.$project['id'])}}" class="">添加配置</a>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>

@endsection

@section('script')
<script>
    $(function () {
      $('select[name="branch"]').on('change', function () {
        deploy_info($(this));
      });
      $('select[name="branch"]').each(function () {
        deploy_info($(this));
      });

    });
    function deploy_info(obj) {
      var branch_info = JSON.parse(obj.children('option:selected').attr('data-commits'));
      var _parent_siblings = obj.parents('.form-group').siblings('.form-group');
      _parent_siblings.find('.last_commit_user').text(branch_info['committer_name'] + '(' + branch_info['committer_email'] + ')')
      _parent_siblings.find('.last_commit_message').text(branch_info['message']);
      _parent_siblings.find('.last_commit_time').text(branch_info['committed_date']);
      _parent_siblings.find('input[name="checkout_sha"]').val(branch_info['id']);
      var web_url = _parent_siblings.find('a.gitlab-compare').data('web-url');
      var last_sha_id = _parent_siblings.find('a.gitlab-compare').data('last-sha-id');
      var compare_url = web_url + '/compare/' + branch_info.id + '...' + last_sha_id;
      _parent_siblings.find('a.gitlab-compare').attr('href', compare_url);
    }
</script>
@endsection


